using System.Collections;
using UnityEngine;

namespace Kit.AnimationEventHelper
{
    public class EventHandler : MonoBehaviour
    {
        [SerializeField] private Animator animator;
        [SerializeField] private AnimationEventHelper animEventHelper;
        [SerializeField] private AudioSource audioSource;

        private void Start()
        {
            animEventHelper.OnAnim = OnAnim;
            animEventHelper.OnSound = OnSound;

            StartCoroutine(RandomBark());
        }
        
        private void OnAnim(string eventName)
        {
            Debug.Log($"OnAnimEvent: {eventName}");
        }

        private void OnSound(string soundPath)
        {
            Debug.Log($"OnSoundEvent: {soundPath}");
            AudioClip ac = Resources.Load(soundPath) as AudioClip;
            audioSource.clip = ac;
            audioSource.loop = false;
            audioSource.Play();
        }

        IEnumerator RandomBark()
        {
            while (true)
            {
                yield return new WaitForSeconds(Random.Range(0.1f, 3f));
                animator.Play("Jump", 0, 0);   
            }
        }
    }
}